﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;
using QxFramework.Core;

public class RotationBalanceFunc : ItemFunc
{
    // Start is called before the first frame update 
    CombinableItem item;
    private float strength;
    [Header("恢复力大小")]
    public float power;
    protected bool IfBalance = false;
    private float StartAngle;
    private float CurrentAngle;
    private float Angle;
    string effectName1;
    string effectName2;
    private float InteractRadius = 1f;
    private GameObject Reciver;

   

    public override void Awake()
    {
        base.Awake();
        item = GetComponent<RotationBalance>();

    }

    public override void KeyDownInvoke()
    {
        base.KeyDownInvoke();
        IfBalance = !IfBalance;
        if (IfBalance)
        {
            StartAngle = item.RotationInDegree;
        }
        else
        {
            StartAngle = 0;
        }

    }
    public override void KeyDownPreference()
    {
        

    }
    public void FixedUpdate()
    {
        //获取刚体
        var torque = item.GetComponentInParent<Rigidbody2D>();

        //获取当前角度
        CurrentAngle = item.RotationInDegree;
        //CurrentAngle = torque.transform.localEulerAngles;
        //Debug.Log(CurrentAngle);

        Angle = CurrentAngle - StartAngle;

        //偏角90度范围内，角度范围不超过0度
        if(Math.Abs(Angle) < 90)
        {
            //调节力矩大小
            strength = - power * Angle;
        }
        //偏角90度范围内，角度范围超过0度
        else if(Math.Abs(Angle) > 270)
        {
            //调节力矩大小
            if(StartAngle < 90 && CurrentAngle > 270)
            {
                strength = power * (360 - Angle);
            }
            else if(StartAngle > 270 && CurrentAngle < 90)
            {
                strength = - power * (360 + Angle);
            }
        }
        //Debug.Log(Angle);   
        //Vector3 s = new Vector3(0, strength, 0);

        if (IfBalance == true)
        {

            //添加力矩
            torque.AddTorque(strength);
            //添加指针特效
            effectName1 = EffectsManager.Instance.Open("pointer", item.transform.position, "pointer_" + item.ItemFieldSetting.ItemName,
              new System.Tuple<string, float>(item.ItemFieldSetting.ItemName, InteractRadius)).name;
            //添加基准线特效
            effectName2 = EffectsManager.Instance.Open("baseline", item.transform.position, "baseline_" + item.ItemFieldSetting.ItemName,
              new System.Tuple<string, float, float>(item.ItemFieldSetting.ItemName, InteractRadius, StartAngle)).name;


        }
      
        else
        {
            if (effectName1 != default)
                EffectsManager.Instance.CloseEffect(effectName1);
            effectName1 = default;
            if (effectName2 != default)
                EffectsManager.Instance.CloseEffect(effectName2);
            effectName2 = default;
        }

    }
    public override void OnDestory()
    {
        base.OnDestory();
        if (effectName1 != default)
            EffectsManager.Instance.CloseEffect(effectName1);
        effectName1 = default;
        if (effectName2 != default)
            EffectsManager.Instance.CloseEffect(effectName2);
        effectName2 = default;
    }
    public override void KeyUpInvoke()
    {
        
    }
}
